<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Favorite
 *
 * @property int $id
 * @property int|null $member_id 用户
 * @property int|null $position_id 工作
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Member|null $member
 * @property-read \App\Models\Position|null $position
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite whereMemberId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite wherePositionId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Favorite whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Favorite extends Model
{
    protected $fillable=['member_id','position_id'];


    public static function boot()
    {
        parent::boot();
        self::addGlobalScope('default_order', function ($query) {
            /** @var Position $query */
            $query->orderBy('created_at','desc');
        });
        self::created(function (Favorite $favorite) {
            $member = \Auth::user();
            foreach ($favorite->position->tags as $tag) {
                $member_portray = $member->portrayals->where('tag_id', $tag->id)->first();
                if ($member_portray instanceof MemberPortrayal) {
                    $member_portray->value = $member_portray->value + 10;
                    $member_portray->save();
                } else {
                    $member_portray = new MemberPortrayal();
                    $member_portray->member()->associate($member);
                    $member_portray->tag()->associate($tag);
                    $member_portray->save();
                }
            }
        });
        self::deleted(function(Favorite $favorite){
            $member = \Auth::user();
            foreach ($favorite->position->tags as $tag) {
                $member_portray = $member->portrayals->where('tag_id', $tag->id)->first();
                if ($member_portray instanceof MemberPortrayal) {
                    $member_portray->value = $member_portray->value - 10;
                    $member_portray->save();
                } else {
                    $member_portray = new MemberPortrayal();
                    $member_portray->member()->associate($member);
                    $member_portray->tag()->associate($tag);
                    $member_portray->save();
                }
            }
        });
    }

    public function member(){
        return $this->belongsTo(Member::class);
    }

    public function position(){
        return $this->belongsTo(Position::class);
    }
}
